var page = sm("do_Page");
var app = sm("do_App");
var hashdata = mm("do_HashData");
var rootview = ui("$");
var nf = sm("do_Notification");
var common = require("common");
var orderUtil = require("url/order");
var qqHttp = require("helper/http");
var openHelper = require("helper/open");
var do_ListView_order = ui("do_ListView_order");
var listdata = mm("do_ListData");
do_ListView_order.bindItems(listdata);
var noData = [{"template":1,"labelContent":"您还没有订单"}];
var pageNum = 1, totalPages = 1;
var orderType = ""; //订单类型，接受其他页面的参数
var param = {};
//andriod返回键
page.on("back", function() {
	//返回刷新
	 app.closePage({"shop_order_list":true})
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage({"shop_order_list":true});
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "店铺订单"
})
header.refreshData();

//右上角添加+图标
var headerRight = ui(addheader + ".headerRight");
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/header/search.png";
imgNext.visible = true;
headerRight.on("touch", function() {
	openHelper.open("source://view/order/search/index.ui");
});

rootview.add("popBox","source://view/order/pop/pop.ui");
var addHaveView = ui("do_ALayout_main").add("haveBorder","source://view/order/supplier/module.ui");
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var data = page.getData();
	if(data.toPage && data.shopId){
		    param.shopId = data.shopId;
		if(data.toPage == "waitSend"){
			param.orderStatus = 1;
			param.payStatus = 2;
			param.deliveryStatus = 1;
		}else if(data.toPage == "waitComplate"){
			param.orderStatus = 1;
			param.payStatus = 2;
			param.deliveryStatus = 2;
		}else if(data.toPage == "hasComplate"){
			param.orderStatus = 2;
			param.payStatus = 2;
		}else if(data.toPage == "error"){
			param.orderStatus = 1;
			param.returnPayStatus = 2;
		}
	}
	refreshAllData();
});
var searchBTime=null,searchETime=null;
page.on("result", function(data) {
	if(null!=data&&null!=data.searchBTime&&null!=data.searchETime){
		var svDate = new Date(data.searchBTime);//要格式化的时间
		searchBTime=svDate.getFullYear()+"-"+(svDate.getMonth()+1)+"-"+svDate.getDate() +" 00:00:00";
		svDate = new Date(data.searchETime);//要格式化的时间
		searchETime=svDate.getFullYear()+"-"+(svDate.getMonth()+1)+"-"+svDate.getDate() +" 23:59:59";
	}else{
		searchBTime=null;
		searchETime=null;
	}
	refreshAllData();
});
//刷新数据
function refreshAllData(){
	pageNum = 1;
	param.page = pageNum;
	param.startOrderStatusAt=searchBTime;
	param.endOrderStatusAt=searchETime;
	qqHttp.post(orderUtil.url.base.shopOrderPage, param, function(data) {
		if(data.page){
			ui(addHaveView + ".haveView").items={"rightView":{"label01":data.completedCount,"label02":data.sellerTotal}};
			totalPages = data.page.totalPages;
			do_ListView_order.rebound();
			listdata.removeAll();
			if (data.page.content.length) {
				img_domain = data.page.content[0].fileDomain;
				listdata.addData(data.page.content);
			}
			else{
				listdata.addData(noData);
			}
		}
		do_ListView_order.bindItems(listdata);
		do_ListView_order.refreshItems();
	})
}

function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		var param = {page:pageNum};
		if (orderType != '') {
			param.orderType = orderType;
		}
		param.startOrderStatusAt=searchBTime;
		param.endOrderStatusAt=searchETime;
		qqHttp.post(orderUtil.url.base.shopOrderPage, param, function(data) {
			if (data.page.content.length > 0) {
				img_domain = data.page.content[0].fileDomain;
			}
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			do_ListView_order.rebound();
			do_ListView_order.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		do_ListView_order.rebound();
		do_ListView_order.refreshItems();
	}
}

//下拉列表，刷新数据
do_ListView_order.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});

//上拉列表，翻页数据
do_ListView_order.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});
